library(cfbfastR)
library(gt)
library(gtExtras)
library(data.table)
library(tidyverse)
library(magrittr)
library(janitor)
library(here)
library(gtUtils)
gt_theme_f5 <- function(gt_object, ...) {
  
  gt_object %>%
    opt_table_font(
      font = list(
        google_font("Roboto"),
        default_fonts()
      ),
      weight = 400
    ) %>%
    tab_style(
      locations = cells_title("title"),
      style = cell_text(
        font = google_font("Roboto"),
        weight = 700
      )
    ) %>%
    tab_style(
      locations = cells_title("subtitle"),
      style = cell_text(
        font = google_font("Roboto"),
        color = "gray65",
        weight = 400
      )
    ) %>%
    tab_style(
      style = list(
        cell_borders(
          sides = "top", color = "black", weight = px(0)
        ),
        cell_text(
          font = google_font("Roboto"),
          #transform = "uppercase",
          v_align = "bottom",
          size = px(14),
          weight = 'bold'
        )
      ),
      locations = list(
        gt::cells_column_labels(),
        gt::cells_stubhead()
      )
    ) %>%
    tab_options(
      column_labels.background.color = "floralwhite",
      data_row.padding = px(7.5),
      heading.border.bottom.style = "none",
      table.border.top.style = "none", # transparent
      table.border.bottom.style = "none",
      column_labels.font.weight = "bold", 
      column_labels.border.top.style = "none",
      column_labels.border.bottom.width = px(2),
      column_labels.border.bottom.color = "black",
      row_group.border.top.style = "none",
      row_group.border.top.color = "black",
      row_group.border.bottom.width = px(1),
      row_group.border.bottom.color = "floralwhite",
      stub.border.color = "floralwhite",
      stub.border.width = px(0),
      source_notes.font.size = 12,
      source_notes.border.lr.style = "none",
      table.font.size = 16,
      heading.align = "left",
      table.background.color = "floralwhite",
      table_body.hlines.color = 'gray90',
      ...
    )
}
stats <- cfbfastR::cfbd_stats_season_player(2024,
                                   season_type = "both",
                                   conference = "MWC") 


rush <- stats |>
  filter(rushing_yds > 500) |>
  mutate(passing_completions = ifelse(is.na(passing_completions), 0, passing_completions)) |>
  filter(passing_completions < 5) |>
  select(team, athlete_id, player, rushing_car, rushing_yds, rushing_td, rushing_long, rushing_ypc, fumbles_lost, fumbles_fum, receiving_rec, receiving_yds, receiving_td, receiving_long)
  

usage <- cfbd_player_usage(2024,
                            conference = "MWC")

rb_usage <- usage |>
  filter(position == "RB") |>
  select(athlete_id, usg_overall, usg_rush, usg_pass)


info <- cfbd_team_info()
## New names:
## 2025-06-09 13:23:00.654278:Invalid arguments or no team data available!
## • `id` -> `id...1`
## • `id` -> `id...14`
rush %<>%
  left_join(info %>% select(school, logo), by = c("team" = "school"))
rbs <- fread(here("data", "2025 MW RBs.csv")) %>%
  clean_names() %>%
  mutate(rank = row_number()) %>%
  arrange(rank) %>%
  mutate(rank = paste0(row_number(), ".")) %>%
  select(rank, everything()) %>%
  mutate(yards = as.numeric(str_remove_all(yards, "[,\\s]"))) %>%
  filter(!is.na(yards)) %>%
  mutate(school = ifelse(school == "San Jose State", "San José State", school)) %>%
  left_join(info %>% select(school, logo), by = c("school" = "school"))
rbs %>%
  select(rank, logo, player, pff_rush_grade, yards, yards_per_attempt,  missed_tackles_forced, notes) %>%
  gt() %>%
  gt_theme_f5() %>%
  gt_img_rows(
    columns = logo,
    height = 30
  ) %>%
  # tab_header(
  #   title = "Top 15 Mountain West RBs",
  #   subtitle = "Entering 2025 Season"
  # ) %>%
   tab_header(
    title = "Top 15 Mountain West RBs Entering 2025",
    subtitle = "Rankings by Nathan Carroll (@Nathan_26_)"
  ) %>%
  tab_options(heading.align = "center") %>%
  cols_label(
    logo = "",
    rank = "Rank",
    player = "Player",
    yards = "Yards",
    yards_per_attempt = "YPA",
    pff_rush_grade = "PFF Grade",
    missed_tackles_forced = "MTF",
    notes = "Notes"
  ) %>%
  #data_color(yards, palette = "rcartocolor::Tropic", domain = c(100, 1400), reverse = T) %>%
  #data_color(yards_per_attempt, palette = "rcartocolor::Tropic", domain = c(3.5, 8), reverse = T) %>%
  data_color(pff_rush_grade, palette = "rcartocolor::Tropic", domain = c(60, 95), reverse = T) %>%
  #data_color(missed_tackles_forced, palette = "rcartocolor::Tropic", domain = c(5, 55), reverse = T) %>%
  tab_options(data_row.padding = '0px') %>%
  # tab_source_note(
  #   source_note = "Data from PFF") %>%
  tab_footnote(
    footnote = "MTF is Missed Tackles Forced",
    locations = cells_column_labels(columns = c(missed_tackles_forced))
  ) %>%
tab_style(
  style = cell_text(size = px(10)),
  locations = cells_body(columns = notes)
) %>%
tab_style(
  style = cell_text(size = px(12)),
  locations = cells_footnotes()
) %>%
  tab_source_note(
    source_note = html("Table by @UnterHonson using <i>The F5</i> Theme, Data by PFF")
  ) #%>%
Top 15 Mountain West RBs Entering 2025
Rankings by Nathan Carroll (@Nathan_26_)
Rank Player PFF Grade Yards YPA MTF1 Notes
1. Jai'Den Thomas 83.5 918 5.6 31
2. Floyd Chalk IV 81.0 735 4.8 45
3. Sire Gaines 66.8 152 7.6 6 Played only 3 games
4. Scottre Humphrey 92.0 1385 6.9 44 At Montana State
5. Malik Sherrod 81.8 967 5.6 34 In 2023 at Fresno State
6. Justin Marshall 78.8 753 4.9 33
7. Bryson Donelson 81.1 476 6.1 17
8. Dylan Carson 70.1 600 4.3 11
9. Jaylon Glover 73.0 562 4.1 26 In 2023 at Utah
10. Christian Washington 81.3 536 5.3 23 At Coastal Carolina
11. Herschel Turner 75.1 407 5.1 19 At Utah State
12. Sam Scott 76.4 435 4.7 17
13. Landon Sims 70.8 351 4.9 17
14. Elijah Gilliam 72.1 457 3.8 18
15. Damon Bankston 80.0 1115 6.2 52 At Weber State
Table by @UnterHonson using The F5 Theme, Data by PFF
1 MTF is Missed Tackles Forced
  #gt_save_crop(file = "RBTop10.png", whitespace = 20, bg = "floralwhite")